<?php
namespace app\login\controller;
use curl\Curl;
use think\Controller;
use wxpay\Wechatapppay;

class Wxpay extends Controller  
{  
    public function initialize(){}
    //统一下单  
    public function unifiedorder($body,$out_trade_no,$total_fee,$notify_url)  
    {   
        // echo $body.$out_trade_no.$total_fee.$notify_url;
        $mytime = time();  
        $wxappid = config('wx_appid');//应用ID 字符串  
        $mch_id = config('wx_mch_id');//商户号 字符串  
        $notify_url = $notify_url;//接收微信支付异步通知回调地址 字符串  
        $wxkey = config('wx_key');//这个是在商户中心设置的那个值用来生成签名时保证安全的 字符串  
        $wechatAppPay = new \wxpay\Wechatapppay($wxappid,$mch_id,$notify_url,$wxkey);   
        $params                     = array();  
        $params['body']             = $body;                            //必填项 商品描述  
        $params['out_trade_no']     = $out_trade_no;                       //必填项 自定义的订单号  
        $params['total_fee']        = ($total_fee*100);                     //必填项 订单金额 单位为分所以要*100  
        // $params['total_fee']        = '1';                                //必填项 订单金额 单位为分所以要*100  
        $params['trade_type']       = 'APP';                                //必填项 交易类型固定写  APP  
        $wx_result = $wechatAppPay->unifiedOrder($params);  
        if($wx_result['return_code'] != "SUCCESS")  
        {  
            return array(  
                'code'=>0,  
                'msg'=>"验签失败,支付失败",
                'data' => $wx_result,
            );  
            // exit;
        }   
        //-------------------------------------------生成签名用到的六个参数-------------------------------------------------------------------  
        $sign_array              = array();  
        $sign_array['appid']     = $wx_result['appid'];    //注意 $sign_array['appid'] 里的参数名必须是appid  
        $sign_array['noncestr']  = $wx_result['nonce_str'];//注意 $sign_array['noncestr'] 里的参数名必须是noncestr  
        $sign_array['package']   = 'Sign=WXPay';           //注意 $sign_array['package'] 里的参数名必须是package  
        $sign_array['partnerid'] = $wx_result['mch_id'];   //注意 $sign_array['partnerid'] 里的参数名必须是partnerid  
        $sign_array['prepayid']  = $wx_result['prepay_id'];//注意 $sign_array['prepayid'] 里的参数名必须是prepayid  
        $sign_array['timestamp'] = $mytime;                 //注意 $sign_array['timestamp'] 里的参数名必须是timestamp  
        //-------------------------------------------生成签名用到的六个参数完毕----------------------------------------------------------------  
        $sign_two = $wechatAppPay->MakeSign($sign_array);//调用wechatAppPay类里的MakeSign()函数生成sign  
  
        //重新组织数组返给app端  
        $myarray=array();  
        $myarray['appid'] = $wx_result['appid'];                  //appid  
        $myarray['partnerid'] = $wx_result['mch_id'];            //商户号  
        $myarray['prepayid'] = $wx_result['prepay_id'];          //预支付交易会话ID  
        $myarray['package'] = "Sign=WXPay";                      //固定值  
        $myarray['noncestr'] = $wx_result['nonce_str'];         //微信返给的随机字符串  
        $myarray['timestamp'] = (string)$mytime;                  //时间  
        $myarray['sign'] =$sign_two;                              //二次签名  
        return ['code' =>1,'mag'=>'签名成功','data'=>$myarray];  
    }  

    //微信支付异步通知回调demo  
    public function apppaysucess()  
    {   
        // $wechatAppPay = new Wechatapppay();
        $mytime = time();  
        $wxappid = config('wx_appid');//应用ID 字符串  
        $mch_id = config('wx_mch_id');//商户号 字符串  
        $notify_url = '';//接收微信支付异步通知回调地址 字符串  
        $wxkey = config('wx_key');//这个是在商户中心设置的那个值用来生成签名时保证安全的 字符串  
        $wechatAppPay = new \wxpay\Wechatapppay($wxappid,$mch_id,$notify_url,$wxkey);
        $data =$wechatAppPay->getNotifyData();//获取数据 用wechatAppPay类里的getNotifyData()方法，这里数据也被getNotifyData()由xml转化成了数组。 
        if(!isset($data['result_code']) || $data['result_code'] != 'SUCCESS') {
            file_put_contents('wxpay.log', file_get_contents('php://input')."\r\n",FILE_APPEND);
            die();
        }
        $w_sign = $data;  
        unset($w_sign['sign']);  
        $verify_sign = $wechatAppPay -> MakeSign($w_sign);//生成验签签名  
        
        if($verify_sign == $data['sign'])  
        {  
            //验签成功  
            //-------------------------逻辑处理----------------------------  
            model('PayOrder')->where(['number'=>$data['out_trade_no']])->update(['status'=>2]);
            $order = model('PayOrder')->where(['number'=>$data['out_trade_no']])->find();
            model('User')->where(['id'=>$order['uid']])->setInc('money',$order['money']);
            //原支付请求的商户订单号  
            // $no= $data['out_trade_no'];  
            //逻辑处理
            //-------------------------逻辑处理完毕------------------------  
            // $wechatAppPay->replyNotify();//通知微信端  
            $str='<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';  
            echo $str;
        }else {  
            //验签失败  
            file_put_contents('wxpay.log', $data['out_trade_no'].' - 验签失败'."\r\n",FILE_APPEND); 
        }  
  
    }

    public function wxpayRefund($out_trade_no, $total_fee)
    {
        $mytime = time();  
        $wxappid = config('wx_appid');//应用ID 字符串  
        $mch_id = config('wx_mch_id');//商户号 字符串  
        $notify_url = '';//接收微信支付异步通知回调地址 字符串  
        $wxkey = config('wx_key');//这个是在商户中心设置的那个值用来生成签名时保证安全的 字符串  
        $wechatAppPay = new \wxpay\Wechatapppay($wxappid,$mch_id,$notify_url,$wxkey);

        $params['out_trade_no'] = $out_trade_no;
        $params['total_fee'] = 1;
        $params['return_oid'] = $out_trade_no;
        $wx_result = $wechatAppPay->refund($params);
        if($wx_result['return_code'] == 'FAIL') {
            return false;
        }
        if($wx_result['result_code'] == 'SUCCESS') {
            return true;
        }
        return false;
    }
  
}  